<?php

namespace App\Repository\Orders;

use App\Models\Admin\AdminUsers;
use App\Models\Orders\OrdersTransferModel as model;
use App\Repository\BaseRepository;

//转单流转处理
class OrdersTransferRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new model();
//    $this->log = new Logger('anchor');
//    $this->log->pushHandler(
//      (new RotatingFileHandler(storage_path('/logs/anchor.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
//    );
        parent::__construct();
    }

    public function logs($id, $page = 1, $limit = 10)
    {
        $ModelUsers = new AdminUsers();
        $rows = [];
        $total = 0;
        if ($page <= 1) {
            $total = $this->model->where(['orders_id' => $id])->count('id');
        }
        foreach ($this->model->from($this->model->getTable() . ' AS a')
                     ->leftJoin($ModelUsers->getTable() . ' AS b', 'a.users_id', '=', 'b.id')
                     ->select('a.orders_id', 'a.created_at', 'a.from_users_name', 'a.remark', 'a.dispose_remark', 'b.nick_name', 'a.status')
                     ->where('a.orders_id', $id)
                     ->page($page, $limit)
                     ->orderBy('a.id', 'desc')
                     ->cursor() as $row) {
            if (!isset($transferArr[$row->orders_id])) {
                $rows[] = $row;
            }
        }
        return $this->returnMsg(200, '操作成功', $rows, $total);
    }

//    处理结果
    public function dispose($postData)
    {
        $mustFieldMsg = [
            'id' => '订单ID不能为空',
            'status' => '结果状态不能没有'
        ];
        $mustField = array_keys($mustFieldMsg);
        $res = $this->getRealData($postData, $mustField, ['remark'], $mustFieldMsg);
        if (!empty($res) && is_string($res)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $res
            );
        }
        $upData = [
            'status' => $postData['status'],
            'dispose_remark' => $postData['remark']
        ];
        $ordersId = $postData['id'];
        $where = [];
        $where['eq']['users_id'] = $postData['user']['id'];
        if (is_array($ordersId)) {
            if (count($ordersId > 1)) {
                $where['in']['orders_id'] = $ordersId;
            } else {
                $where['eq']['orders_id'] = $ordersId[0];
            }
        } else {
            $where['eq']['orders_id'] = $ordersId;
        }
        $res = $this->model->searchWhere($where)->update($upData);
        return $this->returnMsg(200, '标识结果提交成功');
    }

//    流转处理
    public function add($postData)
    {
        $ids = $postData['id'];
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $userId = $postData['user']['id'];
//        本次数据操作只允许的字段
        $mustFieldMsg = [
//            'dept_id' => '部门不能为空',
            'users_id' => '员工不能为空',
            'remark' => '流转说明不能为空'
        ];
        $mustField = array_keys($mustFieldMsg);
        $inData = $this->getRealData($postData, $mustField, [], $mustFieldMsg);

        $inData['from_users_id'] = $userId;
        $inData['from_users_name'] = $postData['user']['nick_name'];
//        转入者信息
        $ModelUsers = new AdminUsers();
        $row = $ModelUsers->where('id', $inData['users_id'])->select(['shop_id', 'nick_name'])->first();
        if (empty($row)) {
            return $this->returnMsg(1, '未查询到员工信息');
        }
        $to_users_name = $row['nick_name'];
        $inData['shop_id'] = $row['shop_id'];
        $inData['status'] = 0;
        $inData['created_at'] = date("Y-m-d H:i:s");
        $inData['updated_at'] = date("Y-m-d H:i:s");
//        批量操作
        foreach ($ids as $ordersId) {
            $inData['orders_id'] = $ordersId;
            if ($this->model->insert($inData)) {
//            如果这个订单是别人转给我的，那么我的流转状态要修改
                $row = $this->model->where(['orders_id' => $ordersId, 'users_id' => $userId, 'status' => 0])->select('id')->first();
                if ($row) {
                    $this->model->where(['id' => $row['id']])->update(['status' => 1]);
                }
//            记录操作日志
                $LogsModel = new \App\Models\LogsModel();
                $logMsg = "流转订单给[" . $to_users_name . "]";
                $LogsModel->saveIt(0, $userId, $ordersId, $logMsg);
            }
        }
        return $this->returnMsg(200, '操作成功');
    }

}
